using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using BlogEngine.Core;

public partial class themes_Refresh_QuoteOfTheDay : System.Web.UI.UserControl
{
  private string dataSource;
  protected void Page_Load(object sender, EventArgs e)
  {
    dataSource = System.Web.HttpContext.Current.Server.MapPath(BlogSettings.Instance.StorageLocation) + "quotes.xml";

    if (Page.User.Identity.IsAuthenticated)
    {
      if (!IsPostBack)
      {
        Quote current = GetCurrentQuote();
        HfQuoteID.Value = current.ID.ToString();
        LblQuote.Text = current.Content;
        LblSource.Text = current.Source;
      }
    }
    else
    {
      if (!IsPostBack)
      {
        Quote current = GetCurrentQuote();
        HfQuoteID.Value = current.ID.ToString();
        LblQuote.Text = current.Content;
        LblSource.Text = current.Source;
      }
    }

  }

  private Quote GetCurrentQuote()
  {
    // Get all Quotes
    List<Quote> quotes = GetQuotes();

    // Check for today's quote
    bool update = true;
    Quote current = new Quote();
    foreach (Quote quote in quotes)
    {
      if (quote.LastDisplayed == DateTime.Now.Date)
      {
        current = quote;
        update = false;
        break;
      }
      else
      {
        if (current.LastDisplayed > quote.LastDisplayed)
          current = quote;
      }
    }

    if (update)
    {
      current.LastDisplayed = DateTime.Now.Date;
      SaveQuotes(current, quotes, "Update");
    }

    return current;

  }

  private List<Quote> GetQuotes()
  {
    List<Quote> quotes = new List<Quote>();

    // Read them in
    if (!File.Exists(dataSource))
    {
      // Load initial entry
      int id = 1;
      string source = "Proverbs 26:14 NKJV";
      string content = "\"As a door turns on its hinges, So does the lazy man on his bed.\"";
      DateTime lastDisplayed = DateTime.Now.Date.AddDays(-1);
      quotes.Add(new Quote(id, source, content, lastDisplayed));
    }
    else
    {
      XmlDocument doc = new XmlDocument();
      doc.Load(dataSource);

      foreach (XmlNode node in doc.SelectNodes("Quotes/Quote"))
      {
        int id = int.Parse(node.SelectSingleNode("id").InnerText);
        string source = node.SelectSingleNode("Source").InnerText;
        string content = node.SelectSingleNode("Contents").InnerText;
        DateTime lastDisplayed = DateTime.Parse(node.SelectSingleNode("LastDisplayed").InnerText);

        Quote temp = new Quote(id, source, content, lastDisplayed);

        quotes.Add(temp);
      }
    }
    return quotes;
  }

  private void SaveQuotes(Quote edit, List<Quote> quotes, string action)
  {
    int currentID = 0;
    // Update quotes
    using (XmlTextWriter writer = new XmlTextWriter(dataSource, System.Text.Encoding.UTF8))
    {
      writer.Formatting = Formatting.Indented;
      writer.Indentation = 4;
      writer.WriteStartDocument(true);
      writer.WriteStartElement("Quotes");

      foreach (Quote quote in quotes)
      {
        currentID = quote.ID;

        if (currentID == edit.ID)
        {
          if (action == "Update")
          {
            writer.WriteStartElement("Quote");

            writer.WriteStartElement("id");
            writer.WriteValue(edit.ID.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("Source");
            writer.WriteValue(edit.Source);
            writer.WriteEndElement();
            writer.WriteStartElement("Contents");
            writer.WriteValue(edit.Content);
            writer.WriteEndElement();
            writer.WriteStartElement("LastDisplayed");
            if (edit.LastDisplayed == DateTime.Now.Date.AddDays(1))
              writer.WriteValue(quote.LastDisplayed.ToShortDateString()); // Use quote to get actual date
            else
              writer.WriteValue(edit.LastDisplayed.ToShortDateString());
            writer.WriteEndElement();

            writer.WriteEndElement();
          }
          // if action == Delete, do nothing.  We don't want to write it back out
        }
        else
        {
          writer.WriteStartElement("Quote");

          writer.WriteStartElement("id");
          writer.WriteValue(quote.ID.ToString());
          writer.WriteEndElement();
          writer.WriteStartElement("Source");
          writer.WriteValue(quote.Source);
          writer.WriteEndElement();
          writer.WriteStartElement("Contents");
          writer.WriteValue(quote.Content);
          writer.WriteEndElement();
          writer.WriteStartElement("LastDisplayed");
          writer.WriteValue(quote.LastDisplayed.ToShortDateString());
          writer.WriteEndElement();

          writer.WriteEndElement();
        }
      }

      if (edit.ID == 0 && action == "Update")
      {
        // We need to add a new one
        currentID++;
        writer.WriteStartElement("Quote");

        writer.WriteStartElement("id");
        writer.WriteValue(currentID.ToString());
        writer.WriteEndElement();
        writer.WriteStartElement("Source");
        writer.WriteValue(edit.Source);
        writer.WriteEndElement();
        writer.WriteStartElement("Contents");
        writer.WriteValue(edit.Content);
        writer.WriteEndElement();
        writer.WriteStartElement("LastDisplayed");
        writer.WriteValue(edit.LastDisplayed.ToShortDateString());
        writer.WriteEndElement();

        writer.WriteEndElement();
      }

      writer.WriteEndElement();
    }
  }

  protected void BtnAdd_Click(object sender, EventArgs e)
  {
    PnlActions.Enabled = false;
    PnlEditor.Visible = true;
    HfEditID.Value = "0";
  }
  protected void BtnEdit_Click(object sender, EventArgs e)
  {
    PnlActions.Enabled = false;
    PnlEditor.Visible = true;
    TxtContent.Text = LblQuote.Text;
    TxtSource.Text = LblSource.Text;
    HfEditID.Value = HfQuoteID.Value;
  }
  protected void BtnDelete_Click(object sender, EventArgs e)
  {
    PnlActions.Enabled = false;
    PnlDelete.Visible = true;
  }
  protected void BtnPrev_Click(object sender, EventArgs e)
  {
    int current = int.Parse(HfQuoteID.Value);
    List<Quote> quotes = GetQuotes();

    foreach (Quote quote in quotes)
    {
      if (quote.ID == current)
        break;
      if (quote.ID < current)
      {
        HfQuoteID.Value = quote.ID.ToString();
        LblQuote.Text = quote.Content;
        LblSource.Text = quote.Source;
      }
    }
  }
  protected void BtnNext_Click(object sender, EventArgs e)
  {
    int current = int.Parse(HfQuoteID.Value);
    List<Quote> quotes = GetQuotes();

    foreach (Quote quote in quotes)
    {
      if (quote.ID > current)
      {
        HfQuoteID.Value = quote.ID.ToString();
        LblQuote.Text = quote.Content;
        LblSource.Text = quote.Source;
        break;
      }
    }

  }
  protected void BtnSave_Click(object sender, EventArgs e)
  {
    int id = int.Parse(HfEditID.Value);
    Quote temp = new Quote(id, TxtSource.Text, TxtContent.Text, DateTime.Now.Date.AddDays(1));
    SaveQuotes(temp, GetQuotes(), "Update");
    LblQuote.Text = TxtContent.Text;
    LblSource.Text = TxtSource.Text;
    TxtContent.Text = "";
    TxtSource.Text = "";
    PnlActions.Enabled = true;
    PnlEditor.Visible = false;
  }
  protected void BtnCancel_Click(object sender, EventArgs e)
  {
    TxtContent.Text = "";
    TxtSource.Text = "";
    PnlActions.Enabled = true;
    PnlEditor.Visible = false;
  }
  protected void BtnYes_Click(object sender, EventArgs e)
  {
    int id = int.Parse(HfEditID.Value);
    Quote temp = new Quote(id, TxtSource.Text, TxtContent.Text, DateTime.Now.Date.AddDays(1));
    SaveQuotes(temp, GetQuotes(), "Delete");
    PnlActions.Enabled = true;
    PnlDelete.Visible = false;
  }
  protected void BtnNo_Click(object sender, EventArgs e)
  {
    PnlActions.Enabled = true;
    PnlDelete.Visible = false;
  }
}

public class Quote
{
  public Quote()
  {
    _id = 0;
    _source = "";
    _content = "";
    _lastDisplayed = DateTime.Now.Date.AddDays(1);
  }
  public Quote(int id, string source, string content, DateTime lastDisplayed)
  {
    _id = id;
    _source = source;
    _content = content;
    _lastDisplayed = lastDisplayed;
  }

  private int _id;

  public int ID
  {
    get { return _id; }
    set { _id = value; }
  }
	

  private string _source;

  public string Source
  {
    get { return _source; }
    set { _source = value; }
  }

  private string  _content;

  public string  Content
  {
    get { return _content; }
    set { _content = value; }
  }

  private DateTime _lastDisplayed;

  public DateTime LastDisplayed
  {
    get { return _lastDisplayed; }
    set { _lastDisplayed = value; }
  }

}